package com.alks.function.mapper.pcfactorywork;

import com.alks.entity.data.entity.PcOutListPrice;
import com.alks.function.data.dto.chengKong.cost.PcOutListPrice2Dto;
import com.alks.function.data.dto.chengKong.cost.SdStyleProcessNewDto;
import com.alks.function.data.dto.pcfactorywork.PcOutListPriceDto;
import com.alks.function.data.request.chengKong.cost.InsertManufacturerRequest;
import com.alks.function.data.request.chengKong.cost.PcOutListPriceUpdateRequest;
import com.alks.function.data.request.factorywork.PcOutListPriceRequest;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 外发工艺单价 Mapper 接口
 * </p>
 *
 * @author ms
 * @since 2023-07-18
 */
public interface PcOutListPriceMapper extends BaseMapper<PcOutListPrice> {

    List<PcOutListPriceDto> listPcOutPrice(@Param("request") PcOutListPriceRequest request, @Param("companyId") String companyId);

    /**
     * 查询外发工艺单价
     * @param request
     * @return
     */
    @Select({
            "<script>",
            "select ID,LOT_NO,STYLE,COLOR,PROCESS,PART_NAME,PUR_MEMO,CFM_FLAG,PRICE,CUT_NUM,VENDOR,PUR_DATE,SEQ_NO,SYS_DATE,SYS_USER from PC_OUT_LIST_PRICE",
            "<where>",
            "and COMPANY_ID = #{companyId} ",
            "<if test='style != null and !style.isEmpty()'>",
            "and STYLE LIKE concat('%', #{style}, '%')",
            "</if>",
            "<if test='lotNo != null and !lotNo.isEmpty()'>",
            "and LOT_NO LIKE concat('%', #{lotNo}, '%')",
            "</if>",
            "<if test='partName != null and !partName.isEmpty()'>",
            "and PART_NAME LIKE concat('%', #{partName}, '%')",
            "</if>",
            "<if test='process != null and !process.isEmpty()'>",
            "and PROCESS LIKE concat('%', #{process}, '%')",
            "</if>",
            "<if test='vendor != null and !vendor.isEmpty()'>",
            "and VENDOR LIKE concat('%', #{vendor}, '%')",
            "</if>",
            "<if test='startSysDate != null and endSysDate != null'>",
            "and PUR_DATE BETWEEN #{startSysDate} AND #{endSysDate}",
            "</if>",
            "<if test='startSysDate != null and endSysDate == null'>",
            "and PUR_DATE &gt;= #{startSysDate}",
            "</if>",
            "<if test='endSysDate != null and startSysDate == null'>",
            "and PUR_DATE &lt;= #{endSysDate}",
            "</if>",
            "<if test='flag != null and flag==0'>",
            "and PRICE is null",
            "</if>",
            "<if test='flag != null and flag==1'>",
            "and PRICE is not  null",
            "</if>",
            "<if test='flag != null and flag==2'>",
            "and LENGTH(SEQ_NO) BETWEEN 1 AND 2 ",
            "</if>",
            "<if test='flag != null and flag==2'>",
            "and LOT_NO = #{lotNo} ",
            "</if>",
            "<if test='flag != null and flag==3'>",
            "and LENGTH(SEQ_NO) = 3 ",
            "</if>",
            "<if test='flag != null and flag==3'>",
            "and LOT_NO = #{lotNo} ",
            "</if>",
            "<if test='process1 != null and process1==0'>",
            "OR  PROCESS in ('冲裁','二次改刀','贴流水标', '二次改刀2')",
            "</if>",
            "<if test='process2 != null and process2==0'>",
            "and PROCESS in(select PROCESS from PC_CUTTING_PROCESS_DETAIL where PROCESS_CODE='01' and PROCESS not in ('冲裁','二次改刀','贴流水标', '二次改刀2'))",
            "</if>",
            "<if test='process3 != null and process3==0'>",
            "and PROCESS in(select PROCESS from PC_CUTTING_PROCESS_DETAIL where PROCESS_CODE='02')",
            "</if>",
            "</where>",
            "order by SYS_DATE,SEQ_NO asc",
            "</script>",
    })
    List<PcOutListPrice2Dto> getPcOutListPriceList(PcOutListPriceRequest request);

    /**
     * 查询外发单价总数
     * @param request
     * @return
     */
    @Select({
            "<script>",
            "select CAST(SUM(PRICE) AS DECIMAL(15, 5)) from PC_OUT_LIST_PRICE",
            "<where>",
            "<if test='style != null and !style.isEmpty()'>",
            "and STYLE LIKE concat('%', #{style}, '%')",
            "</if>",
            "<if test='lotNo != null and !lotNo.isEmpty()'>",
            "and LOT_NO LIKE concat('%', #{lotNo}, '%')",
            "</if>",
            "<if test='partName != null and !partName.isEmpty()'>",
            "and PART_NAME LIKE concat('%', #{partName}, '%')",
            "</if>",
            "<if test='process != null and !process.isEmpty()'>",
            "and PROCESS LIKE concat('%', #{process}, '%')",
            "</if>",
            "<if test='vendor != null and !vendor.isEmpty()'>",
            "and VENDOR LIKE concat('%', #{vendor}, '%')",
            "</if>",
            "<if test='startSysDate != null and endSysDate != null'>",
            "and PUR_DATE BETWEEN #{startSysDate} AND #{endSysDate}",
            "</if>",
            "<if test='startSysDate != null and endSysDate == null'>",
            "and PUR_DATE &gt;= #{startSysDate}",
            "</if>",
            "<if test='endSysDate != null and startSysDate == null'>",
            "and PUR_DATE &lt;= #{endSysDate}",
            "</if>",
            "<if test='flag != null and flag==0'>",
            "and PRICE is null",
            "</if>",
            "<if test='flag != null and flag==1'>",
            "and PRICE is not  null",
            "</if>",
            "<if test='process1 != null and process1==0'>",
            "and PROCESS in ('冲裁','二次改刀','贴流水标', '二次改刀2')",
            "</if>",
            "<if test='process2 != null and process2==0'>",
            "and PROCESS in(select PROCESS from PC_CUTTING_PROCESS_DETAIL where PROCESS_CODE='01' and PROCESS not in ('冲裁','二次改刀','贴流水标', '二次改刀2'))",
            "</if>",
            "<if test='process3 != null and process3==0'>",
            "and PROCESS in(select PROCESS from PC_CUTTING_PROCESS_DETAIL where PROCESS_CODE='02')",
            "</if>",
            "</where>",
            "</script>",
    })
    BigDecimal getPcOutListPriceCountList(PcOutListPriceRequest request);

    /**
     * 新增外发单价建档
     * @param list
     */
    @Insert({
            "<script>",
            "INSERT INTO PC_OUT_LIST_PRICE (COMPANY_ID, SLIP_NO, LOT_NO, STYLE, COLOR, SEQ_NO, PROCESS, PART_NAME, SYS_DATE, SYS_USER, REQ_NO, PUR_MEMO, CFM_FLAG, CFM_DATE, CFM_USER, PRICE, AMT, CUT_NUM, CHECK_FLAG, CHECK_DATE, CHECK_USER, VENDOR, PUR_DATE, MTL_LEVEL) ",
            "VALUES ",
            "<foreach collection='list' item='t' index='index' separator=','>",
            "(#{t.companyId}, #{t.slipNo}, #{t.lotNo}, #{t.style}, #{t.color}, #{t.seqNo}, #{t.process}, #{t.partName}, #{t.sysDate}, #{t.sysUser}, #{t.reqNo}, #{t.purMemo}, #{t.cfmFlag}, #{t.cfmDate}, #{t.cfmUser}, #{t.price}, #{t.amt}, #{t.cutNum}, #{t.checkFlag}, #{t.checkDate}, #{t.checkUser}, #{t.vendor}, #{t.purDate}, #{t.mtlLevel})",
            "</foreach>",
            "</script>"
    })
    void addPcOutListPrice(List<PcOutListPrice> list);

    /**
     * 查询工艺是否检核
     * @param style
     * @param color
     * @param companyId
     * @return
     */
    @Select("select CFM_FLAG FROM SD_STYLE_PROCESS_HEAD WHERE STYLE=#{style} and STYLE_COLOR=#{color} and COMPANY_ID=#{companyId}")
    String getSdStyleProcessHead(@Param("style") String style, @Param("color") String color, @Param("companyId") String companyId);

    /**
     * 查询型体生产工艺
     * @param style
     * @param color
     * @param companyId
     */
    @Select("select STYLE,SEQ_NO,PART_NAME,MTL_NAME,CUT_NUM as pieceNum,CUT_LAYER,STYLE_COLOR,PROCESS1,PROCESS1,PROCESS2,PROCESS3,PROCESS4,PROCESS5,PROCESS6,PROCESS7,PROCESS8,PROCESS9,PROCESS10 from SD_STYLE_PROCESS_NEW where STYLE=#{style} and STYLE_COLOR=#{color} and COMPANY_ID=#{companyId}")
    List<SdStyleProcessNewDto> getSdStyleProcessNewList(@Param("style") String style, @Param("color") String color, @Param("companyId") String companyId);

    /**
     * 插入厂商
     * @param list
     * @param now 建档日期
     */
    @Update({
            "<script>",
            "<foreach collection='list' item='t' separator=';'>",
            "update PC_OUT_LIST_PRICE set VENDOR =#{t.vendor},PUR_DATE = #{now} where ID=#{t.id}",
            "</foreach>",
            "</script>"
    })
    void updateByVendor(@Param("list") List<InsertManufacturerRequest> list,@Param("now") LocalDate now);

    /**
     * 查询是否检核
     * @param id
     * @return
     */
    @Select("select CFM_FLAG FROM PC_OUT_LIST_PRICE where ID=#{id}")
    String getPcOutListPriceFlag(Integer id);

    /**
     * 检核
     * @param list
     * @param userName
     * @param date
     * @param flag
     */
    @Update({
            "<script>",
            "update PC_OUT_LIST_PRICE",
            "set CFM_FLAG = #{flag},CFM_DATE = #{date},CFM_USER=#{userName}",
            "where ID in",
            "<foreach collection='list' item='t' open='(' separator=',' close=')'>",
            "#{t.id}",
            "</foreach>",
            "</script>"
    })
    void updateByCheck(@Param("list") List<InsertManufacturerRequest> list, @Param("userName") String userName, @Param("date") LocalDateTime date, @Param("flag") String flag);

    /**
     * 修改单价和备注
     * @param request
     */
    @Update("update PC_OUT_LIST_PRICE set PRICE=#{price},PUR_MEMO=#{purMemo} where ID=#{id}")
    void PcOutListPrice(PcOutListPriceUpdateRequest request);

}
